# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSlot, QThread
from PyQt5.QtWidgets import QMainWindow, QDialog
import os
import sys
import threading

sys.path.append(os.path.abspath('..'))
from Ui_timeUnit import Ui_Dialog_timeUnit
from Ui_main_window import Ui_MainWindow
from info import phone
from showCopyRight import Dialog_showCopyRight


from tasks import start_test #, save_data


class DialogTimeUnit(QDialog, Ui_Dialog_timeUnit):

    def __init__(self, parent=None):
        super(DialogTimeUnit, self).__init__(parent)
        self.setupUi(self)

    @pyqtSlot()
    def on_pushButton_timeUnit_confirm_clicked(self):
        timeUnit_min = self.radioButton_timeUnit_min.isChecked()
        timeUnit_h = self.radioButton_timeUnit_h.isChecked()
        if timeUnit_min:
            self.time_unit = 1 * 60
        elif timeUnit_h:
            self.time_unit = 1 * 60 * 60
        else:
            # 　默认时间单位为秒
            self.time_unit = 1
        self.close()


class WorkThread(QThread):
    def __init__(self, condition_dict):
        super().__init__()
        self.condition_dict = condition_dict

    def run(self):
        start_test.start(self.condition_dict)


class MainWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

    def pop_dialog_aboutTool(self):
        dialog_about_tools = Dialog_showCopyRight()
        dialog_about_tools.exec_()

    @pyqtSlot()
    def on_toolButton_timeUnit_clicked(self):
        dialog_time_unit = DialogTimeUnit()
        dialog_time_unit.exec_()
        self.time_unit = dialog_time_unit.time_unit
        time_unit_label = u"秒"
        if self.time_unit == 60:
            time_unit_label = u"分钟"
        if self.time_unit == 3600:
            time_unit_label = u"小时"
        self.label_showTimeUnit.setText(time_unit_label)

    @pyqtSlot()
    def on_pushButton_aboutTool_clicked(self):
        self.pop_dialog_aboutTool()

    @pyqtSlot()
    def on_pushButton_startTest_clicked(self):
        try:
            test_time = int(self.time_unit) * int(self.lineEdit_testTime.text())
            condition_dict = {
                'test_time': test_time,
                'package_name': str(self.lineEdit_packageName.text()),
                'is_cpu_test': self.checkBox_cpu.isChecked(),
                'is_mem_test': self.checkBox_memory.isChecked(),
                'is_flow_test': self.checkBox_flw.isChecked(),
                'is_temper_test': self.checkBox_temperature.isChecked(),
                'is_power_test': self.checkBox_power.isChecked()
            }
            self.test_end_flag = False
            self.thread = WorkThread(condition_dict)
            self.label_status.setText(u'正在测试')
            self.thread.start()
            self.thread.wait()

        except AttributeError:
            QtWidgets.QMessageBox.information(self, u"开始测试", u'请将数据填写完整')

    @pyqtSlot()
    def on_pushButton_checkLink_clicked(self):
        self.label_status.setText(u'开始检查设备连接')
        devices_num = phone.get_devices_num()
        '''
        信息框QMessageBox.information
        '''
        if devices_num == "unknown":
            QtWidgets.QMessageBox.information(self, u"检查设备连接", u'没有连接')
            self.label_status.setText(u'设备没有连接')
        else:
            QtWidgets.QMessageBox.information(self, u"检查设备连接", u'已经连接')
            self.label_status.setText(u'设备已经连接')

    def show_phone_info(self):
        phone_info_dict = phone.get_phone_info()
        phone_info = u"""
                API   : %s
                设备号 : %s
                手机型号 : %s
                系统版本 : %s
                手机品牌 : %s
                手机分辨率 : %s
                CPU内核数 : %s
                """ % (phone_info_dict['api'],
                       phone_info_dict['devices_num'],
                       phone_info_dict['model'],
                       phone_info_dict['version'],
                       phone_info_dict['brand'],
                       phone_info_dict['pix'],
                       phone_info_dict['cpu_kel_num']
                       )
        self.label_showPhoneInfo.setText(phone_info)
        self.label_status.setText(u"更新手机基本信息")

    @pyqtSlot()
    def on_pushButton_phoneInfo_clicked(self):
        thread_show_phone_info = threading.Thread(target=self.show_phone_info)
        thread_show_phone_info.start()
        self.label_status.setText(u"开始获取手机基本信息")

    @pyqtSlot()
    def on_pushButton_saveData_clicked(self):
        try:
            if self.thread.isFinished():
                self.label_status.setText(u'开始保存数据')
                save_data.write_execl()
                self.label_status.setText(u'保存数据成功')
        except AttributeError:
            QtWidgets.QMessageBox.information(self, u"保存数据", u'数据测试未结束')


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())
